package com.example.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.example.model.HistoryListAdapter;
import com.example.model.HistoryModel;
import com.example.test.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class TopSiteView extends Activity {
	private ListView toplist;
	private HistoryModel hisModel;
	private Cursor mCursor;
	private ImageButton back;
	private int History_ID = 0;
	private View itemView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.topsite_view);
		hisModel = new HistoryModel(this);
		
		Calendar c = Calendar.getInstance();
		SimpleDateFormat tf = new SimpleDateFormat("H");
		String timef = tf.format(c.getTime());
		int t = Integer.parseInt(timef);
		String timeFrame = null;
		if (t >= 6 && t <= 8)

			timeFrame = "Morning";

		else if (t >= 9 && t < 11)
			timeFrame = "Antemeridiem";
		else if (t >= 11 && t < 13)
			timeFrame = "LunchTime";
		else if (t >= 13 && t < 17)
			timeFrame = "Afternoon";
		else if (t >= 17 && t < 19)
			timeFrame = "DinnerTime";
		else if (t >= 1 && t < 23)
			timeFrame = "Evening";
		else
			timeFrame = "Midnight";
		TextView tff=(TextView)findViewById(R.id.text_tf);
		tff.setText(timeFrame);
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String location = bundle.getString("CURLOC");
		TextView lcc=(TextView)findViewById(R.id.text_lc);
		lcc.setText(location);

		mCursor = hisModel.sort(timeFrame, location);
		
		toplist=(ListView)findViewById(R.id.list_top);
		toplist.setAdapter(new HistoryListAdapter(this,mCursor));
		
		back=(ImageButton)findViewById(R.id.btn_back_top);
		back.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				finish();
				
			}
			
		});
		toplist.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long id) {
				itemBackChanged(view);
				mCursor.moveToPosition(position);
				String url=mCursor.getString(1);
				String curLoc=mCursor.getString(4);
				Intent intent = new Intent(TopSiteView.this, BrowserView.class);
				Bundle bundle = new Bundle();
				
				System.out.printf(url, curLoc);
				bundle.putString("CURLOC", curLoc);
				bundle.putString("URL", url);

				intent.putExtras(bundle);
				startActivity(intent);
				
				
			}
			
		});
		

	}
	private void itemBackChanged(View view) {

		view.setBackgroundResource(android.R.color.holo_red_light);

		itemView = view;
	}
}
